<?php

//构造函数与析构函数
    class newFish1{
        var $name;
        var $weight;
        function __construct($name,$weight){
            $this->name = $name;
            $this->weight = $weight;
        }

        function getFish(){
            echo $this->name,"重量为",$this->weight;
            return ' 如果用echo打印这个函数就会出现这行字';
        }

        function __destruct(){
            echo '这行字会在销毁这个对象时出现，如果没有销毁的动作则出现在最下面';
        }


}

    $Fish1=new newFish1('鲤鱼','20kg');
    echo $Fish1->getFish(),PHP_EOL;
    $Fish1->getFish();
    echo PHP_EOL,'233',PHP_EOL;
    unset($Fish1);

?>


<?php

//访问与重写
    class newFishing{
        var $fish;
        var $fisherNum;
        function __construct($num){
            $this->fish=['鲤鱼','草鱼','鲢鱼',];
            $this->fisherNum=$num;
        }
        private function fishing($fishNum){
            echo $this->fish[$fishNum];
        }
        public function getFish(){
            echo '你是钓手',$this->fisherNum;
            echo '，你钓到了：';
        }

        public function fishNotice($x){
            $this->getFish();
            $this->fishing($x);
        }
    }

    $fisher1=new newFishing(1);
    $fisher1->fishNotice(1);
    echo PHP_EOL;

    class newFishing2 extends newFishing{
//        重写了钓鱼方法
        public function fishing($fishNum){
            if(isset($this->fish[$fishNum+1])){
                echo $this->fish[$fishNum+1];
            }
            else{
                echo "没有这条鱼！";
            }
        }

        public function getFish(){
            echo '你是钓手',$this->fisherNum;
            echo '，你钓到的是：';
        }
    }
//public方法被重写了，private方法没有
    $fisher2=new newFishing2(2);
    $fisher2->fishNotice(1);
    echo PHP_EOL;
    echo $fisher2->fishing(1),PHP_EOL;
    echo $fisher2->fishing(2);








?>
